using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BepInEx;
using HarmonyLib;
using Hearthstone;
using Hearthstone.Progression;
using PegasusLettuce;
using PegasusShared;
using PegasusUtil;
using UnityEngine;

namespace Mercenary
{
    public static class UtilsStra
    {
        public static IStrategy m_pveNormalStra = new StraPveNormal();
        public static IStrategy m_autoTaskStra = new StraAutoTask();
        public static IStrategy m_autoMapStra = new StraAutoMap();
        public static IStrategy m_pvpElderGodsStra = new StraPvpElderGods();
        public static IStrategy m_pvpNormalStra = new StraPvpNormal();

        //根据名称寻找策略
        public static IStrategy GetStrategy(string name)
        {
            if(name==m_pveNormalStra.Name()) return m_pveNormalStra;
            else if(name==m_autoTaskStra.Name()) return m_autoTaskStra;
            else if(name==m_autoMapStra.Name()) return m_autoMapStra;
            else if (name == m_pvpElderGodsStra.Name()) return m_pvpElderGodsStra;
            else if (name == m_pvpNormalStra.Name()) return m_pvpNormalStra;
            return m_pveNormalStra;
        }

        //更新佣兵队列
        public static void UpdateMercList()
        {
            GetStrategy(UtilsGui.strategyConf.m_name).UpdateMercList();
        }

        //根据名称寻找序号
        public static int FindMercenaryIndexForName(string name, List<MercCard> mercList)
        {
            return mercList.FindIndex((MercCard m) => m.m_name == name);
        }

        //根据ID找到佣兵
        public static MercCard FindMercenaryForId(int id, List<MercCard> mercList)
        {
            return mercList.Find(m => m.m_id==id);
        }
     
        //根据名称找佣兵
        public static MercCard FindMercenaryForName(string name, List<MercCard> mercList)
        {
            return mercList.Find(m => m.m_name.Contains(name) || name.Contains(m.m_name));
        }

        //根据ID找技能
        public static MercSkill FindSkillForId(int id, MercCard mercenary)
        {
            return mercenary.m_skills.Find(s => s.m_id==id);
        }

        //根据名称找技能
        public static MercSkill FindSkillForName(string name, MercCard mercenary)
        {
            return mercenary.m_skills.Find(
                s => s.m_name.Contains(name) || name.Contains(s.m_name));
        }

        //根据名称找目标
        public static MercCard FindTargetForName(List<MercCard> targetEntities,string preKill)
        {
            return targetEntities.Find(
                i => i.m_name.Contains(preKill) || preKill.Contains(i.m_name));
        }

        //寻找速度最慢的人
        public static MercCard FindSlowestTarget(List<MercCard> targetEntities)
        {
            if (targetEntities.Count == 0) return null;
            var speed = targetEntities.Max(i => i.m_curSpeed);
            return targetEntities.Find(i => i.m_curSpeed == speed);
        }

        //寻找当前生命值最高的人
        public static MercCard FindMaxHealthTarget(List<MercCard> targetEntities)
        {
            if (targetEntities.Count == 0) return null;
            var health = targetEntities.Max(i => i.m_curHealth);
            return targetEntities.Find(i => i.m_curHealth == health);
        }

        //寻找当前生命值最低的人
        public static MercCard FindMinHealthTarget(List<MercCard> targetEntities)
        {
            if (targetEntities.Count == 0) return null;
            var health = targetEntities.Min(i => i.m_curHealth);
            return targetEntities.Find(i => i.m_curHealth == health);
        }

        //寻找掉血最多的人
        public static MercCard FindMaxLossHealthTarget(List<MercCard> targetEntities)
        {
            MercCard result = null;
            if (targetEntities.Count > 0)
            {
                int losshealth = Enumerable.Max<MercCard>(targetEntities,
                    (MercCard i) => i.m_defHealth - i.m_curHealth);
                if (losshealth > 0)
                {
                    result = targetEntities.Find(
                        (MercCard i) => i.m_defHealth - i.m_curHealth == losshealth);
                }
            }
            return result;
        }

        public static MercCard FindSpecNameTarget(string name, List<MercCard> targetEntities)
        {
            if (targetEntities.Count == 0) return null;
            return targetEntities.Find((MercCard i) => i.m_name.Contains(name));
        }

        public static MercCard GetTarget(List<MercCard> targetEntities)
        {
            foreach (var target in targetEntities)
            {
                if (target == null)
                    continue;

                if (target.m_enable == false)
                    continue;

                return target;
            }
            return null;
        }

        /*
        public static MercCard[] GetQuickMercenary(int mercenaryId)
        {
            List<MercCard> list = new List<MercCard>();
            foreach (LettuceAbility lettuceAbility in
                CollectionManager.Get().GetMercenary((long)mercenaryId,false,true).m_abilityList)
            {
                CardDbfRecord cardRecord =
                    GameDbf.GetIndex().GetCardRecord(lettuceAbility.GetCardId());
                string @string = cardRecord.TextInHand.GetString(true);
                if (@string != null && @string.Contains("速度值") && @string.Contains("加快"))
                {
                    string string2 = cardRecord.Name.GetString(true);
                    list.Add(new MercCard(mercenaryId,0));
                }
            }
            return list.ToArray();
        }
        */
    }
}
